package com.vvv.controller;

import cn.hutool.core.bean.BeanUtil;
import com.vvv.common.BaseResponse;
import com.vvv.model.dto.UserRequest;
import com.vvv.model.entity.User;
import com.vvv.model.vo.LoginUserVo;
import com.vvv.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author vvv
 * @date 2023-12-09 15 25
 * discription
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public BaseResponse<LoginUserVo> login(@RequestBody UserRequest userRequest,
                                           HttpServletRequest request) {
        String userAccount = userRequest.getUserAccount();
        String userPassword = userRequest.getUserPassword();
        LoginUserVo loginUserVo = userService.login(userAccount, userPassword, request);
        return BaseResponse.success(loginUserVo);
    }

    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody UserRequest userRequest) {
        String userAccount = userRequest.getUserAccount();
        String userPassword = userRequest.getUserPassword();
        long result = userService.register(userAccount, userPassword);
        return BaseResponse.success(result);
    }

    @GetMapping("/get/login")
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return BaseResponse.success(BeanUtil.copyProperties(user, LoginUserVo.class));
    }
}
